Make sure DryRunnable does not affect normal run, and test #dry_run? also

Akinori MUSHA 9 年之前
父节点
当前提交
cc4966daf4
共有 1 个文件被更改,包括 21 次插入3 次删除
  1. 21 3
      spec/concerns/dry_runnable_spec.rb

+ 21 - 3
spec/concerns/dry_runnable_spec.rb

@@ -11,7 +11,7 @@ describe DryRunnable do
11 11
       create_event payload: { 'test' => 'foo' }
12 12
       error "Recording error"
13 13
       create_event payload: { 'test' => 'bar' }
14
-      self.memory = { 'last_status' => 'ok' }
14
+      self.memory = { 'last_status' => 'ok', 'dry_run' => dry_run? }
15 15
       save!
16 16
     end
17 17
   end
@@ -28,7 +28,25 @@ describe DryRunnable do
28 28
     [users(:bob).agents.count, users(:bob).events.count, users(:bob).logs.count]
29 29
   end
30 30
 
31
-  it "traps logging, event emission and memory updating" do
31
+  it "does not affect normal run, with dry_run? returning false" do
32
+    before = counts
33
+    after  = before.zip([0, 2, 2]).map { |x, d| x + d }
34
+
35
+    expect {
36
+      @agent.check
37
+      @agent.reload
38
+    }.to change { counts }.from(before).to(after)
39
+
40
+    expect(@agent.memory).to eq({ 'last_status' => 'ok', 'dry_run' => false })
41
+
42
+    payloads = @agent.events.reorder(:id).last(2).map(&:payload)
43
+    expect(payloads).to eq([{ 'test' => 'foo' }, { 'test' => 'bar' }])
44
+
45
+    messages = @agent.logs.reorder(:id).last(2).map(&:message)
46
+    expect(messages).to eq(['Logging', 'Recording error'])
47
+  end
48
+
49
+  it "traps logging, event emission and memory updating, with dry_run? returning true" do
32 50
     results = nil
33 51
 
34 52
     expect {
@@ -40,7 +58,7 @@ describe DryRunnable do
40 58
 
41 59
     expect(results[:log]).to match(/\AI, .+ INFO -- : Logging\nE, .+ ERROR -- : Recording error\n/)
42 60
     expect(results[:events]).to eq([{ 'test' => 'foo' }, { 'test' => 'bar' }])
43
-    expect(results[:memory]).to eq({ 'last_status' => 'ok' })
61
+    expect(results[:memory]).to eq({ 'last_status' => 'ok', 'dry_run' => true })
44 62
   end
45 63
 
46 64
   it "does not perform dry-run if Agent does not support dry-run" do